package com.jx.gocomtask.webservice.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.jx.gocomtask.utils.JacksonUtil;
import com.jx.gocomtask.webservice.DTO.Token;


@Service
public class SystemSuperApp {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SystemSuperApp.class);
	private static Map<String,Token> map=new ConcurrentHashMap<> ();
	private  ReentrantLock lock = new ReentrantLock();  
	private static Token token=new Token();
	@Autowired
	RedisService redisService;
	@Autowired
	RestTemplate restTemplate;
	@Autowired
	MultiValueMap<String, String> superappConfig;
	@Resource(name="apiurl")
	String gocomeApiUrl;
	private Token create_accessToken(String superappId,String secret){
		Token token_=new Token(); 
		 MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
		requestEntity.add("appid", superappId);
		requestEntity.add("secret", secret);
		String r=restTemplate.postForObject(gocomeApiUrl+"superapp/access_token",requestEntity,String.class);
		token_=JacksonUtil.readValue(r, Token.class); 
		token_.setExpiresClock(System.currentTimeMillis()+(token.getExpires()-1200)*1000);
		map.put(superappId, token_);
		return token_;
	}
	public Token accessToken(String superappId,String secret){
		 lock.lock();
			try{ 
				long sc=System.currentTimeMillis();
				Token token_=map.get(superappId);
				 if(token_==null){
					 return this.create_accessToken(superappId, secret);
				 }else{
					 if(token_.getExpiresClock()>sc && redisService.exists(token_.getAccessToken())){
							return token_;
					}else{
						return  this.create_accessToken(superappId, secret);
					}
				 }
				 
				//return token_;
			}finally{  
		         lock.unlock();//释放锁  
		    }
	}
	public Token accessToken(){
		lock.lock();
		try{ 
			long sc=System.currentTimeMillis();
			if(token.getExpiresClock()>sc){
				//logger.info("==token.getExpiresClock()=={}=====sc=={}====",token.getExpiresClock(),sc);
				if(redisService.exists(token.getAccessToken())){
					return token;
				}
			}
			String r=restTemplate.postForObject(gocomeApiUrl+"superapp/access_token",superappConfig,String.class);
			token=JacksonUtil.readValue(r, Token.class); 
			token.setExpiresClock(System.currentTimeMillis()+(token.getExpires()-1200)*1000);
			logger.info(r);
			return token;
		}finally{  
	         lock.unlock();//释放锁  
	      }
	}
	public String appid_token(String appid){
		return appid_token(this.accessToken().getAccessToken(),appid);
	}

	private String appid_token(String tokenStr,String appid){
		StringBuffer sb=new StringBuffer();
		sb.append("access_token=");
		sb.append(tokenStr);
		sb.append("&");
		sb.append("token_type=");
		sb.append("superapp");
		sb.append("&");
		sb.append("appid=");
		sb.append(appid);
		return sb.toString();
	}
//String superappId,String secret
	public String appid_token(String superappId,String secret,String appid){
		Token t=this.accessToken(superappId, secret);
		logger.info("--->{}",t.getAccessToken());
		return appid_token(t.getAccessToken(),appid);
	}
}
